<!-- CreatePatient.vue -->
<template>
    <div class="min-h-screen bg-gray-100 flex justify-center items-center">
        <var-loading description="LOADING" color="#006263" :loading="loading">
            <div class="bg-white p-8 shadow-lg rounded-lg w-[25vw]">
                <h2 class="text-lg font-semibold mb-4">创建患者档案</h2>
                <var-form ref="form" scroll-to-error="start">
                    <var-input v-model="formData.visitCardId" placeholder="请输入患者就诊卡号" focus-color="#006263" :rules="[(v) => !!v || '就诊卡号不能为空']" class="mb-4" />
                    <var-input v-model="formData.name" placeholder="请输入患者姓名" focus-color="#006263" :rules="[(v) => !!v || '姓名不能为空', (v) => (v && v.trim().length >= 2) || '姓名至少为两个字']" class="mb-4" />
                    <var-input v-model="formData.phone" placeholder="请输入患者手机号" focus-color="#006263" :rules="[(v) => /^\d{11}$/.test(v) || '必须为11位数字']" class="mb-4" />
                    <var-input v-model="formData.age" placeholder="请输入患者年龄" focus-color="#006263" :rules="[(v) => !!v || '年龄不能为空', (v) => /^[1-9]\d*$/.test(v) || '年龄必须为正整数', (v) => (v > 0 && v < 120) || '年龄必须在1到119之间']" type="number" class="mb-4" />
                    <var-select v-model="formData.sex" placeholder="请选择性别" focus-color="#006263" :rules="[(v) => !!v || '请选择性别']" class="mb-4">
                        <var-option label="男" value="MAN" />
                        <var-option label="女" value="WOMAN" />
                    </var-select>
                    <div class="flex gap-4">
                        <var-button text-color="white" color="#EE8080" size="large" block @click="$emit('back')">返回</var-button>
                        <var-button text-color="white" color="#006263" size="large" block @click="handleSubmit">提交注册</var-button>
                    </div>
                </var-form>
            </div>
        </var-loading>
        <var-dialog message="是否要为该患者创建就诊记录？" confirm-button-text-color="#006263" cancel-button-text-color="#006263" v-model:show="show" @confirm="handleConfirm" />
    </div>
</template>

<script>
import { uuidv7 } from 'uuidv7';
export default {
    data() {
        return {
            loading: false,
            show: false,
            patientId: '',
            formData: {
                visitCardId: '',
                name: '',
                phone: '',
                age: '',
                sex: ''
            }
        };
    },
    methods: {
        async handleSubmit() {
            const valid = await this.$refs.form.validate();
            if (!valid) {
                this.$api.notify.error('表单验证失败，请检查输入');
                return;
            }
            this.loading = true;
            const res = await this.$api.patient.createPatient(this.formData);
            this.loading = false;
            if (res.success) {
                this.patientId = res.data;
                this.$api.notify.success('患者档案创建成功');
                this.show = true;
            } else {
                this.$api.notify.error(res.error);
            }
        },
        handleConfirm() {
            const id = uuidv7();
            let sexStr = '';
            if (this.formData.sex === 'MAN') {
                sexStr = '男';
            } else if (this.formData.sex === 'WOMAN') {
                sexStr = '女';
            }
            this.$store.record.recordId = id;
            this.$store.record.visitCardId = this.formData.visitCardId;
            this.$store.record.patientId =this.patientId;
            this.$store.record.name = this.formData.name;
            this.$store.record.phone = this.formData.phone;
            this.$store.record.age = this.formData.age;
            this.$store.record.sex = sexStr;
        }
    }
};
</script>
